package mvpfoot.presenter;

import android.util.Log;


import java.io.IOException;

import mvpfoot.entity.Constant;
import mvpfoot.entity.News;
import mvpfoot.entity.Stores;
import mvpfoot.entity.ZhiHuDaily;
import mvpfoot.iViewBind.IPresenter;
import mvpfoot.model.NetworkApi;
import okhttp3.Call;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Chen qin lang on 2016/12/27.
 */
public abstract class BasePresenter implements IPresenter {


    public final Retrofit retrofit;
    private String TAG="dasdas";
    public static int i=0;
    @Override
    public abstract void showError();
    private int maxage=60*60;
    public final NetworkApi networkApi;

    class LoginInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Request build = request.newBuilder().removeHeader("Cache-contronl")
                    .addHeader("Cache-control", "public, max-age" + maxage)
                    .build();

            i++;
            Response proceed = chain.proceed(build);
            Log.d(TAG, "intercept() called with: " + "chain = [" + proceed.headers() + "]");
            return proceed;
        }
    }
    public BasePresenter() {
        OkHttpClient client=new OkHttpClient()
                .newBuilder()
                .addInterceptor(new LoginInterceptor())
                .addNetworkInterceptor(new LoginInterceptor())
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        networkApi = retrofit.create(NetworkApi.class);
    }


    public class MyCallBack implements Callback<ZhiHuDaily> {
        @Override
        public void onResponse(retrofit2.Call<ZhiHuDaily> call, retrofit2.Response<ZhiHuDaily> response) {
            boolean successful = response.isSuccessful();
            if (successful){
                pareseData(response);
            }else{
                showError();
            }
        }

        @Override
        public void onFailure(retrofit2.Call call, Throwable t) {

        }
    }


}
